package gatperdut.chato.lucene.indexer;

import gatperdut.chato.constants.CSConstantsLucene;
import gatperdut.chato.lucene.indexer.commands.CommandsIndexer;
import gatperdut.chato.lucene.indexer.mechanisms.MechanismsIndexer;

import java.io.File;
import java.io.IOException;

import javax.xml.stream.XMLStreamException;

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class IndexEverything {

    private static int totalIndexed = 0;

    public static void main( String[] args ) throws IOException, XMLStreamException {

        IndexWriter writer = createWriter();

        indexCommands( writer );

        indexMechanisms( writer );

        writer.close();

    }

    private static IndexWriter createWriter() throws IOException {
        File indexesFile = new File( CSConstantsLucene.INDEX_PATH );
        Directory indexesDirectory = FSDirectory.open( indexesFile );

        SimpleAnalyzer sa = new SimpleAnalyzer( Version.LUCENE_35 );
        IndexWriterConfig iwc = new IndexWriterConfig( Version.LUCENE_35, sa );
        iwc.setOpenMode( IndexWriterConfig.OpenMode.CREATE );

        return new IndexWriter( indexesDirectory, iwc );
    }

    private static void indexCommands( IndexWriter writer ) throws IOException, XMLStreamException {
        CommandsIndexer ci = new CommandsIndexer( writer );

        int numIndexed = ci.index();

        System.out.println( "Indexed " + ( numIndexed - totalIndexed ) + " command files." );

        totalIndexed += numIndexed;
    }

    private static void indexMechanisms( IndexWriter writer ) throws IOException, XMLStreamException {
        MechanismsIndexer mi = new MechanismsIndexer( writer );

        int numIndexed = mi.index();

        System.out.println( "Indexed " + ( numIndexed - totalIndexed ) + " mechanism files." );

        totalIndexed += numIndexed;
    }
}
